package xrz.com.jr;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Map;

import myhttps.Http;
import myhttps.HttpConst;
import myhttps.HttpListener;
import myhttps.HttpUtil;
import xrz.com.jr.adapter.Adapter_Nomal;
import xrz.com.jr.entity.NomalModel;
import xrz.com.jr.utils.ExitUtil;
import xrz.com.jr.utils.OpenBrowUtils;

/**
 * Created by Administrator on 2016/7/28.
 */
public class Act_NomalModel extends BaseActivity implements AdapterView.OnItemClickListener{

    private ListView listview;
    private Adapter_Nomal adapter;
    private String id;
    private ImageView top_back_img;
    private TextView top_tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_nomalmodel);
        listview = (ListView)findViewById(R.id.listview);
        listview.setOnItemClickListener(this);
        adapter = new Adapter_Nomal(this);
        listview.setAdapter(adapter);
        id = getIntent().getStringExtra("id");
        top_tv = (TextView)findViewById(R.id.top_tv);
        top_back_img = (ImageView)findViewById(R.id.top_back_img);
        top_tv.setText(getIntent().getStringExtra("title"));
        top_back_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getData();
        ExitUtil.getInstant().addToPool(this);
    }

    private void getData(){
        Map<String,String> map = HttpUtil.getParams("id",id);
        Http.getInstant().sendRequest(this, HttpConst.getModelDetailList, map, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                List<NomalModel> list = JSON.parseArray(result, NomalModel.class);
                adapter.clear();
                adapter.addAll(list);
            }

            @Override
            public void onHttpError(String error) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        NomalModel nomal = adapter.getItem(i);
        String link = nomal.getLink();
        OpenBrowUtils.startBrower(this, link);
    }
}
